// system.h 
//	All global variables used in Nachos are defined here.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef SYSTEM_H
#define SYSTEM_H

#include "copyright.h"
#include "utility.h"
#include "thread.h"
#include "scheduler.h"
#include "interrupt.h"
#include "stats.h"
#include "timer.h"
#include "synch.h"


#define MAX_LOCKS 		150	// maximum number of locks that can be in the system at once
#define MAX_CONDITIONS 	150	// maximum number of conditions that can be in the system at once
#define MAX_PROCESSES	150	// maximum number of processes that can be in the system at once
#define MAX_THREADS		50 	// maximum number of threads that can fit in the address space of a single process

class AddrSpace;

struct SysLock {

public:
	
	SysLock(Lock* l, AddrSpace* process) {
		lock = l;
		flaggedForDelete = false;
		processID = process;
	}
	
	Lock *lock;
	bool flaggedForDelete;
	AddrSpace* processID;
	
};

struct SysCondition {
public:
	SysCondition(Condition* c, AddrSpace* process) {
		condition = c;
		flaggedForDelete = false;
		processID = process;
	}

	Condition *condition;
	bool flaggedForDelete;
	AddrSpace* processID;
};

/*struct SysProcess {
public:
	SysProcess(AddrSpace* addr) {
		space = addr;
		numActiveThreads = 1; // Init to 1 because the main process thread is active at this point.
	}

	AddrSpace* 	space;
	int numActiveThreads;
};*/

// Initialization and cleanup routines
extern void Initialize(int argc, char **argv); 	// Initialization,
						// called before anything else
extern void Cleanup();				// Cleanup, called when
						// Nachos is done.

extern Thread *currentThread;			// the thread holding the CPU
extern Thread *threadToBeDestroyed;  	// the thread that just finished
extern Scheduler *scheduler;			// the ready list
extern Interrupt *interrupt;			// interrupt status
extern Statistics *stats;				// performance metrics
extern Timer *timer;					// the hardware alarm clock
extern int tlbCounter;				// eviction counter for the TLB 

#ifdef USER_PROGRAM
#include "machine.h"
#include "table.h"
extern Machine* machine;			// user program memory and registers
extern Table* 	lockTable;			// table of all the locks registered with the OS
extern Table* 	conditionTable;		// table of all the conditions registered with the OS
extern Table	*physicalPageTable; // Table of spaceIDs indicating which of the pages in main physical memory are current assigned to an address space.
extern Table*	invertedPageTable;	// table of PageTableEntries that will record what is currently in each physical memory page
extern char* swapFileName;			// file name of the swap file. Currently hard codded.
extern OpenFile* swapFile;			// Open file handle for the swap file.
extern Lock* swapMapLock;			// Lock to protect the swapfile bit map.
extern BitMap* swapBitMap;			// Bit map to record which pages in the swap file are currently occupied.
extern const int SWAPFILESIZE;		// Total size of the swap file.
//extern int* processTable;	//table of processes and threads
//extern int numActiveProcesses; //int of the number of active processes that have threads
#endif

#ifdef FILESYS_NEEDED 		// FILESYS or FILESYS_STUB 
#include "filesys.h"
extern FileSystem  *fileSystem;
#endif

#ifdef FILESYS
#include "synchdisk.h"
extern SynchDisk   *synchDisk;
#endif

#ifdef NETWORK
#include "post.h"
extern PostOffice* postOffice;
#endif

#endif // SYSTEM_H
